package leetcode.每日一题;

import leetcode.util.CommonUtils;
import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author ：zsy
 * @date ：Created 2022/11/4 11:25 AM
 * @description：
 */
public class 到达终点数字 {
    @Test
    public void test() {
        System.out.println(new Solution().reachNumber(50));
    }

    class Solution {
        public int reachNumber(int target) {
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(0);
            int step = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    Integer curIdx = queue.poll();
                    if (curIdx == target) return step;
                    queue.offer(curIdx + step + 1);
                    queue.offer(curIdx - step - 1);
                }
                step++;
            }
            return step;
        }
    }
}
